/*
Audica Uploader - application for convenient uploading to various file hostings
Copyright (C) 2011-2012 Makarov Andrey (Winand)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "uploadsummary.h"
#include "ui_uploadsummary.h"

UploadSummary::UploadSummary(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UploadSummary)
{
    ui->setupUi(this);
    QStringList headers;
    headers << tr("URL") << tr("Description");
    modresult.setHorizontalHeaderLabels(headers);
    ui->tvOutput->setModel(&modresult);
    ui->tvOutput->setColumnWidth(0, ui->tvOutput->width() / 2);
    connect(ui->tvOutput->selectionModel(), SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), SLOT(itemSelected(const QModelIndex&, const QModelIndex&)));
}

UploadSummary::~UploadSummary() {
    delete ui;
}

void UploadSummary::addUploadResult(const QString filepath, UploadResult *result) {
    /*for(int i = 0; i < result->urls.length(); i++)
        qDebug() << result->urls[i] << result->descriptions[i];*/
    QStandardItem* item = new QStandardItem(filepath);
    for(int i = 0; i < result->urls.length(); i++) {
        QList<QStandardItem*> url;
        url << new QStandardItem(result->urls[i])
            << new QStandardItem(result->descriptions[i]);
        item->appendRow(url);
    }
    modresult.appendRow(item);
    ui->tvOutput->expand(item->index());
    ui->tvOutput->resizeColumnToContents(0);
}

void UploadSummary::itemSelected(const QModelIndex &current, const QModelIndex &previous) {
    qDebug() << modresult.itemFromIndex(current)->text() << current.row() << current.column();
    QString url;
    if(current.parent().isValid())
        url = modresult.itemFromIndex(current.sibling(current.row(), 0))->text();
    else {
        QModelIndex firstUrl = current.child(0, 0);
        if(firstUrl.isValid())
            url = modresult.itemFromIndex(firstUrl)->text();
    }
    ui->txtOutput->setText(url);
}
